 <script type="text/javascript">
  function trim ( str, charlist ) {
          charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
          var re = new RegExp('^[' + charlist + ']+|[' + charlist + ']+$', 'g');
          return str.replace(re, '');
      } 

  jQuery(document).ready(function(){
    $("#btn_submit").click(function(){
      var error = false;
      var message = 'Это поле обязательное';
      $('.error_msg').html('');
      
      if (trim($('#name_news').val()) == ""){
        error = true;
        $('#name_news_error').html(message);
      }

       if (trim($('#sum_news').val()) == ""){
        error = true;
        $('#sum_news_error').html(message);
      }

      if (trim($('#body_news').val()) == ""){
        error = true;
        $('#body_news_error').html(message);
      }

      if (trim($('#tags_news').val()) == ""){
        error = true;
        $('#tags_news_error').html(message);
      }
            
      var re = new RegExp("\.(gif|jpg|png|jpeg)$","ig");
      var path = $('#pict_file').val();
      if (path != '') {
          var test_image = path.match(re);
          if (!test_image){
              $('#pict_file_error').html("неправильный формат изображения! используйте: *.gif, *.jpg, *.png!");
              return;
          }
      }
      if(error == true){return}
      $('#form_news').submit();
    });
  });
 <?php
    if(isset($data['data_news'])){ $data_news = $data['data_news']; }
    if(isset($data['categories'])){ $categories = $data['categories']; }
 ?>
</script>
<div class="hero-unit admin_style">
  <form action="/admin/save_edit_news<?php if(isset($data_news['news_id'])){?>?<?=$data_news['news_id'];}?>" 
  method="POST" id="form_news" ENCTYPE="multipart/form-data"> 
    <table id="table_form_edit">
       <tr>
        <td>
          <label for="name_news">Название новости <span class="required">*<span></label>
        </td>
        <td><input type="text" name="name" id="name_news" class="span12" 
        value="<?if(isset($data_news['name'])){?><?=htmlspecialchars($data_news['name'])?><?}?>"></td>
        <td><span class="error_msg" id="name_news_error"><span></td>
      </tr>
      <tr>
        <td><label for="sum_news">Анонс <span class="required">*<span></label></td>
        <td>
          <textarea name="sum" id="sum_news" ><?if(isset($data_news['summary_news'])){?><?=htmlspecialchars($data_news['summary_news'])?><?}?></textarea>
        </td>
        <td><span class="error_msg" id="sum_news_error"><span></td>
      </tr>
      <tr>
        <td><label for="body_news">Текст <span class="required">*<span></label></td>
        <td>
          <textarea name="body" id="body_news"><?if(isset($data_news['body'])){?><?=htmlspecialchars($data_news['body'])?><?}?></textarea>
        </td>
        <td><span class="error_msg" id="body_news_error"><span></td>
      </tr>
       <tr>
        <td></td>
        <td>Вводите теги через запятые</td>
        <td></td>
      </tr>
      <tr>
        <td><label for="tags_news">Теги <span class="required">*<span></label></td>
        <td><input type="text" name="tags" id="tags_news" class="span12"
         value="<?if(isset($data_news['tags'])){?><?=htmlspecialchars($data_news['tags'])?><?}?>"></td>
        <td><span class="error_msg" id="tags_news_error"><span></td>
      </tr>
      <?php if(count($categories) > 0){?>
      <tr>
        <td><label for="categories">Категория</label></td>
        <td>
          <select id="categories" name="cat">
            <?php foreach ($categories as $key => $value) { ?>
               <option value="<?=$value['cat_id']?>"
               <?if (isset($value['cat_id']) AND ($data_news['category_id'] == $value['cat_id']))
               {?>selected<?}?>
               ><?=htmlspecialchars($value['name'])?></option>
            <?php } ?>
          </select>
        </td>
        <td><span class="error_msg" id="categories_error"><span></td>
      </tr>
      <?}?>
      <?php if(isset($data_news['pict'])){?>
      <tr>
        <td></td>
        <td><img src="/public/img/<?=$data_news['pict']?>" alt="Изображение" width="120" height="120"></td>
        <td></td>
      </tr>
      <?}?>
      <tr>
        <td><label for="">Загрузить изображение</label></td>
        <td><input type="file" name="pict" id="pict_file"></td>
        <td><span class="error_msg" id="pict_file_error"><span></td>
      </tr>
      <tr>
        <td><label for="show_hide">Показывать</label></td>
        <td>
          <input type="checkbox" id="show_hide" name="vis" value="show" 
          <?if((isset($data_news['visibility']) AND $data_news['visibility'] == 'show') or ($data['action'] == 'add')){?>checked<?}?>>
        </td>
        <td><span class="error_msg" id="show_hide_error"><span></td>
      </tr>
    </table>
    <div class="cont_button">
      <?if($data['action'] == 'edit'){?>
        <a class="btn btn-danger btn-large" id="btn_submit">Редактировать</a>
      <?}else{?>
        <a class="btn btn-danger btn-large" id="btn_submit">Добавить</a>
      <?}?>
        <a href="/admin" class="btn btn-warning" data-dismiss="modal">Нет, давай назад</a>
    </div>
  </form>
</div> 
